package com.feng.sharedingjdbc.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gufusheng
 * @time 2021/7/24 17:51
 */
@Slf4j
public class DBUtils {


    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;
    private static Connection connection;
    private static String[] shardingTableNames;
    private static String active;

    static {
        active = YmlUtils.getYmlConfig("application.yml", "spring.profiles.active");
        try {
            driverClass = YmlUtils.getYmlConfig("sharding.jdbc.datasource.sharedb0.driver-class-name");
            url = YmlUtils.getYmlConfig("sharding.jdbc.datasource.sharedb0.url");
            user = YmlUtils.getYmlConfig("sharding.jdbc.datasource.sharedb0.username");
            password = YmlUtils.getYmlConfig("sharding.jdbc.datasource.sharedb0.password");

            Class.forName(driverClass);
            connection = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        shardingTableNames = YmlUtils.getYmlConfig("term.sharding.tables").split(",");
    }

    /**
     * 执行sql
     *
     * @param sql sql语句
     * @throws Exception
     */
    public void executeSql(String sql) throws Exception {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.execute();
            log.info("sql执行成功..");
        } catch (SQLException e) {
            throw new Exception("sql执行失败.." + '\n' + "SQL: " + sql);
        }
    }

    /**
     * 获取数据库中所有数据表
     *
     * @return
     */
    public List<String> tableNames() {
        StringBuffer sbTables = new StringBuffer();
        List<String> tables = new ArrayList<String>();
        try {
            DatabaseMetaData dbMetaData = connection.getMetaData();
            ResultSet rs = dbMetaData.getTables(null, null, null, new String[]{"TABLE"});
            while (rs.next()) {// ///TABLE_TYPE/REMARKS
                //            sbTables.append("表名：" + rs.getString("TABLE_NAME") + "<br/>");
                //            sbTables.append("表类型：" + rs.getString("TABLE_TYPE") + "<br/>");
                //            sbTables.append("表所属数据库：" + rs.getString("TABLE_CAT") + "<br/>");
                //            sbTables.append("表所属用户名：" + rs.getString("TABLE_SCHEM")+ "<br/>");
                //            sbTables.append("表备注：" + rs.getString("REMARKS") + "<br/>");
                if ("share0".equals(rs.getString("TABLE_CAT"))) {
                    tables.add(rs.getString("TABLE_NAME"));
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return tables;
    }
}
